﻿using AntMovement;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Prism.Mvvm;
using System.Diagnostics;

namespace ExeOne
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title;

        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public int Port { get; set; } = 22345;
        public string IP { get; set; } = "127.0.0.1";
        public int Port1 { get; set; } = 22346;
        public TestDataModel Data { get; set; }
        public ClassDataModel ClassEntity { get; set; }
        private ITagManager _tagManager;

        public MainWindowViewModel()
        {
            Data = new TestDataModel();
            ClassEntity = new ClassDataModel() { D1 = 5.36, Index = 999 };
            _tagManager = new TagManager();
            _tagManager.Parsing.AddTag("Data", Data);
            _tagManager.Parsing.AddTag("ClassEntity", ClassEntity);
            Title = $"当前程序进程ID：[{Process.GetCurrentProcess().Id}]";
        }

        private bool _disMonitor = true;
        public DelegateCommand Monitor => new DelegateCommand(() =>
        {
            AntService uDPService = new AntService(_tagManager, Port);
            if(uDPService.Open())
            {
                MessageBox.Show("监听成功...");
                _disMonitor = false;
            }
            else
            {
                MessageBox.Show("监听失败...");
            }
        },() => _disMonitor);

        private AntClient _antClient;
        private bool _disConnect = true;
        public DelegateCommand Connect => new DelegateCommand(() =>
        {
            _antClient = new AntClient(IP, Port1, new System.Threading.CancellationTokenSource());
            if (_antClient.Open())
            {
                MessageBox.Show("连接成功");
                _disConnect = false;
            }
            else
            {
                MessageBox.Show("连接失败");
            }
        }, () => _disConnect);

        public int Index { get; set; }
        public string Name { get; set; }

        public DelegateCommand<string> Send => new DelegateCommand<string>(sender =>
        {
            if (_antClient == null)
            {
                MessageBox.Show("未连接服务器!!!");
            }
            switch (sender)
            {
                case "1":
                    _antClient.WriteSingle(Index, "Data.Index");
                    break; 
                case "2":
                    _antClient.WriteSingle(Name, "Data.Name");
                    break;
            }
        });

        private string _classData;

        public string ClassData
        {
            get { return _classData; }
            set { SetProperty(ref _classData, value); }
        }

        public DelegateCommand ReadClas => new DelegateCommand(() =>
        {
            if (_antClient == null)
            {
                MessageBox.Show("未连接服务器!!!");
            }
            try
            {
                ClassData = System.Text.Json.JsonSerializer.Serialize(_antClient.ReadClass<ClassDataModel>("ClassEntity"));
            }
            catch (Exception ex)
            {
                ClassData = $"异常：{ex.Message}";
            }
        });
    }
}
